﻿CROSS_COMPILE 	?= riscv-nuclei-elf-
TARGET		  	?= longan

RISCV_ARCH := rv32im
RISCV_ABI := ilp32
RISCV_MCMODEL := medlow

DEFS       = -DUSE_STDPERIPH_DRIVER -DHXTAL_VALUE=8000000U
CFLAGS    += $(DEFS)

CFLAGS += -march=$(RISCV_ARCH)
CFLAGS += -mabi=$(RISCV_ABI)
CFLAGS += -mcmodel=$(RISCV_MCMODEL) -ffunction-sections -fdata-sections -fno-builtin-printf -fno-builtin-malloc
#CFLAGS += -march=rv32imac -mabi=ilp32 -mcmodel=medlow -msmall-data-limit=8 -mno-save-restore -Os -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -fno-common  -g
AS 				:= $(CROSS_COMPILE)as		
CC 				:= $(CROSS_COMPILE)gcc
LD				:= $(CROSS_COMPILE)ld
OBJCOPY 	:= $(CROSS_COMPILE)objcopy
OBJDUMP 	:= $(CROSS_COMPILE)objdump
SIZE      := $(CROSS_COMPILE)size

export INCDIRS SRCDIRS

#windows 10 libpath
#LIBPATH			:= -lgcc -L D:\RISC-V_EmbeddedGnuToolchain\lib\gcc\riscv-nuclei-elf\9.2.0
#linux  libpath
#LIBPATH			:= -lgcc -L /usr/lib/RISC-V_EmbeddedGnuToolchain/lib/gcc/riscv-nuclei-elf/9.2.0

INCDIRS 		+= GD32VF103_standard_peripheral/Include \
								GD32VF103_standard_peripheral \
								include \
								RISCV/drivers \
								third_party/rt-thread-3.1.3 \
								third_party/rt-thread-3.1.3/components/finsh \
								third_party/rt-thread-3.1.3/libcpu/risc-v/bumblebee \
								third_party/rt-thread-3.1.3/libcpu/risc-v/common \
								third_party/rt-thread-3.1.3/include \
								lib \
								#common 
								
#third_party/letter-shell
			   
SRCDIRS			+= GD32VF103_standard_peripheral \
								GD32VF103_standard_peripheral/Source \
								RISCV/drivers \
								RISCV/env_Eclipse \
								RISCV/stusb \
								lib \
								third_party/rt-thread-3.1.3/components/finsh \
								third_party/rt-thread-3.1.3/libcpu/risc-v/bumblebee \
								third_party/rt-thread-3.1.3/libcpu/risc-v/common \
								third_party/rt-thread-3.1.3/src \
								src \
								#common 
								
#给每个参数添加 -I			   
INCLUDE			:= $(patsubst %, -I %, $(INCDIRS))

#获得每个文件的路径
# $(wildcard pattern)  wildcard 会以 pattern 这个格式，去寻找存在的文件，返回存在文件的名字
SFILES			:= $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.S))
CFILES			:= $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.c))
HFILES			:= $(foreach dir, $(INCDIRS), $(wildcard $(dir)/*.h))
#notdir 将路径去掉 只剩文件名
#SFILENDIR = start.S
SFILENDIR		:= $(notdir  $(SFILES))
CFILENDIR		:= $(notdir  $(CFILES))
INCLUDEFILE	:= $(notdir  $(HFILES))

#$(patsubst pattern,replacement,\$(var))
#patsubst 函数是从 var 变量里面取出每一个值，如果这个符合 pattern 格式，把它替换成 replacement 格式
SOBJS			:= $(patsubst %, obj/%, $(SFILENDIR:.S=.o))
COBJS			:= $(patsubst %, obj/%, $(CFILENDIR:.c=.o))
#所有的 *.o文件
OBJS			:= $(SOBJS) $(COBJS)
VPATH			:= $(SRCDIRS)

LINKER_SCRIPT := RISCV/env_Eclipse/GD32VF103xB.lds
LDFLAGS += -T $(LINKER_SCRIPT) -nostartfiles -Wl,--gc-sections -Wl,--check-sections
LINK_DEPS += $(LINKER_SCRIPT)

.PHONY: clean show_value
	
$(TARGET): $(OBJS) $(LINK_DEPS)
	$(CC) $(CFLAGS) $(INCLUDES) $(OBJS) -o $@.elf $(LDFLAGS)
	$(OBJCOPY) -O binary $@.elf $@.bin
	$(OBJDUMP) --disassemble-all $@ > $@.dump
	$(SIZE) $(TARGET).elf

#编译所有汇编文件
$(SOBJS) : obj/%.o : %.S
	$(CC) $(CFLAGS) $(INCLUDE) -c -o $@ $<

#编译所有c文件
$(COBJS) : obj/%.o : %.c
	$(CC) $(CFLAGS) $(INCLUDE) -c -o $@ $<


clean:
	rm -rf $(TARGET).elf $(TARGET).dis $(TARGET).bin $(COBJS) $(SOBJS) $(TARGET).hex $(TARGET).dump

show_value:
	@echo INCLUDE=$(INCLUDE)
	@echo SOBJS = ${SOBJS}
	@echo COBJS = ${COBJS}
	@echo CFILES = ${CFILES}
	@echo SFILES = ${SFILES}
	@echo SFILENDIR = $(SFILENDIR)
	@echo CFILENDIR = $(CFILENDIR)
	@echo LIBPATH = $(LIBPATH)
	@echo SUBDIRS = $(SUBDIRS)
	@echo OBJS = $(OBJS)
	@echo INCLUDEFILE = $(INCLUDEFILE)
